package com.infonautics.action;

import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.hibernate.Userinfo;
import com.hibernate.Unit;
import com.hibernate.Userrole;
import com.infonautics.dao.IUserinfoDao;
import com.infonautics.dao.imp.IUserinfoDaoImp;
import com.infonautics.page.PageBean;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.system.dao.ISystemDao;

public class UserinfoAction extends ActionSupport implements Preparable,SessionAware{
	private static final long serialVersionUID = 1L;
	private Map<String,Object> session;
	private String pageUrl = "/WEB-INF/userinfo/";
	private String successMsg;
	private String url;
	private String errormessage;
	private Userinfo userinfo;
	private ISystemDao<Userinfo> userinfoDao = new IUserinfoDaoImp();
	private IUserinfoDao userDao = new IUserinfoDaoImp();
	private Userinfo user;
	private PageBean<Userinfo> pageBean;
	private int page = 1;
	private List<Unit> unitList;
	private List<Userrole> userroleList;
	private int id;
	private String username;
	
	@Override
	public void prepare() throws Exception {
		if(session.containsKey("userinfo")){
			if(session.get("userinfo")!=null){
				userinfo=(Userinfo)session.get("userinfo");
			}
		}
		
	}
	
	//查询管理员列表
	public String list() throws Exception{
		pageBean = userDao.doUserinfoList(20, page);
		return "input";
	}

	public String query () throws Exception{
		String queryString = "";
		if(username != null && !username.equals("")){
			username = new String(username.getBytes("ISO8859-1"),"UTF-8");
			queryString += "and username like '%" + username + "%'";
		}
		pageBean = userDao.doUserinfoList(20, page, queryString);
		pageBean.setQueryString(queryString);
		return "list";
	}
	//新增管理员页面
	public String addPage()throws Exception{
		unitList = userDao.doUnitList();
		userroleList = userDao.doUserroleList();
		return "input";
	}
	
	//新增管理员
	public String add() throws Exception{
		userinfoDao.doAdd(user);
		doMsgHandle("添加成功","Userinfo_list.action");
		return UserinfoAction.SUCCESS;
	}
	
	//修改管理员页面
	public String modifyPage() throws Exception{
		user = userinfoDao.doOne(id);
		unitList = userDao.doUnitList();
		userroleList = userDao.doUserroleList();
		return "input";
	}
	//修改管理员信息
	public String modify() throws Exception{
		userinfoDao.doModfiy(user);
		doMsgHandle("修改成功","Userinfo_list.action");
		return UserinfoAction.SUCCESS;
		
	}
	//删除管理员
	public String delete() throws Exception{
		userinfoDao.doDelete(id);
		doMsgHandle("删除成功","Userinfo_list.action");
		return UserinfoAction.SUCCESS;
	}
	//个人设置页面
	public String settingPage() throws Exception{
		user = userinfoDao.doOne(id);
		return "input";
	}
	//个人设置
	public String setting() throws Exception{
		userinfoDao.doModfiy(user);
		doMsgHandle("设置成功","Userinfo_settingPage.action?id="+user.getId());
		return UserinfoAction.SUCCESS;
	}
	//服务方法
	private void doMsgHandle(String successMsg,String url){
		this.successMsg=successMsg;
		this.url=url;
	}
	
	public Map<String, Object> getSession() {
		return session;
	}
	
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	public String getErrormessage() {
		return errormessage;
	}
	
	public void setErrormessage(String errormessage) {
		this.errormessage = errormessage;
	}
	
	public String getPageUrl() {
		return pageUrl;
	}

	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}

	public Userinfo getUserinfo() {
		return userinfo;
	}
	
	public void setUserinfo(Userinfo userinfo) {
		this.userinfo = userinfo;
	}
	
	public Userinfo getUser() {
		return user;
	}
	
	public void setUser(Userinfo user) {
		this.user = user;
	}
	
	public String getSuccessMsg() {
		return successMsg;
	}
	
	public void setSuccessMsg(String successMsg) {
		this.successMsg = successMsg;
	}
	
	public String getUrl() {
		return url;
	}
	
	public void setUrl(String url) {
		this.url = url;
	}

	public PageBean<Userinfo> getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean<Userinfo> pageBean) {
		this.pageBean = pageBean;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public List<Unit> getUnitList() {
		return unitList;
	}

	public void setUnitList(List<Unit> unitList) {
		this.unitList = unitList;
	}

	public List<Userrole> getUserroleList() {
		return userroleList;
	}

	public void setUserroleList(List<Userrole> userroleList) {
		this.userroleList = userroleList;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	
	
}
